package aom.ju.ss.view.cut

import android.app.ActivityOptions
import android.content.Intent
import android.os.Bundle
import android.util.Pair
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import aom.ju.ss.R
import aom.ju.ss.databinding.CutGoodsItemBinding
import aom.ju.ss.databinding.CutHomeFragmentBinding
import aom.ju.ss.mode.CutGoodsMode
import aom.ju.ss.mode.CutGoodsModeData
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.dp
import aom.ju.ss.utils.http.LP_API.get_list
import aom.ju.ss.utils.show
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.BaseListFragment
import aom.ju.ss.view.base.TopBar
import aom.ju.ss.view.shua.HomeShuaFragment
import com.google.gson.Gson

/**
 * 砍价 商品列表
 */
class CutGoodsFragment : BaseListFragment() {

    init {
        isShow = false
    }

    var type = "0"

    companion object {
        fun getMys(type: String): CutGoodsFragment {
            val f = CutGoodsFragment()
            val b = Bundle()
            b.putString("type", type)
            f.arguments = b
            return f
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        type = requireArguments().getString("type", "1")
        return super.onCreateView(inflater, container, savedInstanceState)

    }

    override fun bind(binding: ViewDataBinding, position: Int) {
        super.bind(binding, position)
        val bin = binding as CutGoodsItemBinding
        val info = getList()[position] as CutGoodsModeData
        bin.info = info
        bin.type = type

        bin.img.setOnClickListener {
            if (type == "3") {
                "开发中".show
                return@setOnClickListener
            }
            val pair = Pair<View, String>(bin.img, "aa")
            val activity = activity as AppCompatActivity
            val bundle: Bundle =
                ActivityOptions.makeSceneTransitionAnimation(activity, pair).toBundle()
            activity.startActivity(
                Intent(activity, CutGoodsDetailsActivity::class.java).putExtra(
                    "data",
                    info.id.toString()
                ).putExtra(
                    "img",
                    info.image
                ), bundle
            )
        }
        bin.rootView.setOnClickListener {
            when (type) {
                "2" -> {//砍价
                    bin.img.performClick()
                }
                "3" -> {//抢券
                    bin.img.performClick()
                }
                "1" -> {//品牌
                    bin.img.performClick()
                }
            }
        }

    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, CutGoodsMode::class.java)
            setList(mode.data)
    }

    override fun LayoutManager(): RecyclerView.LayoutManager {
        return GridLayoutManager(activity, 2)
    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show
    }

    override fun item(): Int {
        return R.layout.cut_goods_item
    }

    override fun key(): Array<String> {
        return arrayOf("type", "page")
    }


    //订单类型  1普通订单  2砍价订单  3抢券订单  4易货订单  5易货促销订单 ，购物车下单默认普通订单，暂不支持其他类型
    override fun value(): Array<String> {
        return arrayOf(type, page.toString())
    }

    override fun api(): String {
        return get_list
    }


}